package com.css.fxfzaqbz.modules.data.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.css.fxfzaqbz.modules.data.entity.ActiveFaultData;
import com.css.fxfzaqbz.modules.data.queryParams.ActiveFaultDataQueryParams;
import com.css.fxfzaqbz.modules.data.repository.ActiveFaultDataRepository;

import com.css.fxfzaqbz.modules.data.service.ActiveFaultDataService;
import com.css.fxfzaqbz.util.FileUtil2;
import com.css.fxfzaqbz.util.PlatformDateUtils;
import com.css.fxfzaqbz.util.PlatformPageUtils;
import com.css.fxfzaqbz.util.UUIDGenerator;
import com.css.fxfzaqbz.constants.YNEnum;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.sql.Timestamp;
import java.util.List;

@Service
public class ActiveFaultDataServiceImpl implements ActiveFaultDataService {
    @Resource
    ActiveFaultDataRepository activeFaultDataRepository;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ActiveFaultData updateActiveFaultData(ActiveFaultData activeFaultData) {

        activeFaultData.setAfdUpdatetime(PlatformDateUtils.getCurrentTimestamp());

        return activeFaultDataRepository.updateActiveFaultData(activeFaultData);
    }

    @Override
    @Transactional
    public ActiveFaultData saveActiveFaultData(ActiveFaultData activeFaultData) {
        activeFaultData.setAfdId(UUIDGenerator.getUUID());
        activeFaultData.setAfdStatus(YNEnum.Y.toString());
        activeFaultData.setAfdCreatetime(PlatformDateUtils.getCurrentTimestamp());
        return activeFaultDataRepository.saveActiveFaultData(activeFaultData);
    }

    @Override
    public JSONObject getAllActiveFaultData(ActiveFaultDataQueryParams queryParams) {
        return PlatformPageUtils.formatPageData(activeFaultDataRepository.getAllActiveFaultDataParams(queryParams));
    }

    @Override
    public ActiveFaultData getActiveFaultDataById(String id) {
        ActiveFaultData activeFaultData = activeFaultDataRepository.getActiveFaultDataById(id);
        return activeFaultData;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteActiveFaultDatas(List<String> ids) {
        Timestamp timestamp = PlatformDateUtils.getCurrentTimestamp();
        activeFaultDataRepository.deleteActiveFaultDatasById(ids,timestamp);
    }

    @Override
    public List<ActiveFaultData> getAll() {

        return activeFaultDataRepository.getAll();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void importExcel(MultipartFile file) throws IOException {
        List<ActiveFaultData> activeFaultData = FileUtil2.importExcel(file, ActiveFaultData.class);
        for(int i = 0 ; i<activeFaultData.size();i++){
            activeFaultData.get(i).setAfdId(UUIDGenerator.getUUID());
            activeFaultData.get(i).setAfdStatus(YNEnum.Y.toString());
            activeFaultData.get(i).setAfdCreatetime(PlatformDateUtils.getCurrentTimestamp());
        }
        activeFaultDataRepository.importExcel(activeFaultData);
    }
}
